﻿Public Class oSalesPerson
    Public Function UpdateJsonSalesPerson(ByVal salesPersonID As String, _
                                        ByVal salesPersonCode As String, ByVal salesPersonName As String, ByVal companyCode As String, _
                                        ByVal createdUserId As String, ByVal managerID As String, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim dbManagerID As Object
                If managerID.Length = 0 Then
                    dbManagerID = DBNull.Value
                Else
                    dbManagerID = managerID
                End If

                Dim params = New Object() {companyCode, salesPersonCode, salesPersonName, dbManagerID, createdUserId}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oSalesPerson]")
                    query.Append("   ([CompanyCode]")
                    query.Append(" ,[SalesPersonCode]")
                    query.Append(" ,[SalesPersonName]")
                    query.Append(" ,[ManagerID]")
                    query.Append(" ,[CreatedUserID])")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oSalesPerson] SET ")
                    query.Append("  [CompanyCode] = @Param1")
                    query.Append("  ,[SalesPersonCode] =  @Param2")
                    query.Append("  ,[SalesPersonName] =  @Param3")
                    query.Append("  ,[ManagerID] =  @Param4")
                    query.Append(" WHERE [SalesPersonID] = @Param5")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {companyCode, salesPersonCode, salesPersonName, dbManagerID, salesPersonID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonSalesPerson(ByVal dataBaseName As String, ByVal salesPersionID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {salesPersionID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oSalesPerson]")
                query.Append("  WHERE oSalesPerson.SalesPersonID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllSalesPerson(ByVal dataBaseName As String, ByVal companyCode As String, ByVal isAdmin As Boolean) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oSalesPerson")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                If isAdmin Then
                    query.Append("SELECT T0.*,T3.SalesPersonCode AS ManagerCode,T3.SalesPersonName AS ManagerName,T1.*")
                    query.Append("  FROM oSalesPerson  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  ")
                    query.Append(" JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                    query.Append(" LEFT JOIN oSalesPerson T3 ON T0.ManagerID = T3.SalesPersonID")
                    query.Append(" ORDER BY T0.SalesPersonID DESC")
                    ds = connect.ObjectGetAll_Query_SAP(query.ToString())
                Else
                    query.Append("SELECT T0.*,T3.SalesPersonCode AS ManagerCode,T3.SalesPersonName AS ManagerName,T1.*")
                    query.Append("  FROM oSalesPerson  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  ")
                    query.Append(" JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                    query.Append(" LEFT JOIN oSalesPerson T3 ON T0.ManagerID = T3.SalesPersonID")
                    query.Append(" WHERE T0.CompanyCode = @Param1")
                    query.Append(" ORDER BY T0.SalesPersonID DESC")
                    ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {companyCode})
                End If
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetSalesPersonByID(ByVal dataBaseName As String, ByVal salesPersonID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oSalesPerson")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("SELECT T0.*,T3.SalesPersonCode AS ManagerCode,T3.SalesPersonName AS ManagerName,T1.*")
                query.Append("  FROM oSalesPerson  T0 JOIN oCompany   T1 ON T0.CompanyCode = T1.CompanyCode  ")
                query.Append("  JOIN oUser T2 ON T0.CreatedUserID = T2.UserID ")
                query.Append(" LEFT JOIN oSalesPerson T3 ON T0.ManagerID = T3.SalesPersonID")
                query.Append("  WHERE T0.SalesPersonID = @Param1 ")
                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {salesPersonID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
